package com.zyw.dev.zyw.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zyw.dev.zyw.adapter.api.AdapterListener;

import java.util.List;

/**
 * 基类的适配器，适用于列表，九宫格等
 * Created by Misl on 2016/1/6.
 */
public abstract class BaseViewAdapter<T> extends BaseAdapter implements AdapterListener<T> {


    /**
     * 数据list
     */
    protected List<T> dataList;

    /**
     * 上下文环境
     */
    protected Context context;

    /**
     * 布局界面
     */
    protected LayoutInflater layoutInflater;

    /**
     * 布局的id
     */
//    protected int layoutId;

    /**
     * 设置选中项
     */
    protected int selectedIndex;

    /**
     * 构造函数
     * @param context
     * @param dataList
     */
    public BaseViewAdapter(Context context, List<T> dataList/*, int layoutId*/){
        this.context = context;
        this.dataList = dataList;
//        this.layoutId = layoutId;
        this.layoutInflater = LayoutInflater.from(context);
        selectedIndex = -1;
    }

    /**
     * 获取设置的上下文环境
     * @return
     */
    public Context getContext() {
        return context;
    }

    /**
     * 设置数据列表
     * @return
     */
    public void setDataList(List<T> dataLists) {
        dataList = dataLists;
        notifyDataSetChanged();
    }

    /**
     * 获取数据列表
     * @return
     */
    public List<T> getDataList() {
        return dataList;
    }

    @Override
    public int getCount() {
        return dataList == null?0:dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList == null?null:dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 设置选中项
     * @param index
     */
    public void setSelectedIndex(int index){
        selectedIndex = index;
    }

    /**
     * 获取选中项
     */
    public int getSelectedIndex(){
        return selectedIndex;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        if (convertView == null){
//            convertView = layoutInflater.inflate(layoutId,null);
//        }
        if (dataList!=null && dataList.size()>0){
            bindToView(position,convertView,dataList.get(position));
        }
        return convertView;
    }

    /**
     * 通过inflater创建view
     * @param layoutId
     * @param parent
     * @return
     */
    public View getViewByInflate(int layoutId,ViewGroup parent){
        return layoutInflater.inflate(layoutId,parent);
    }

    /**
     * 设置TextView的文本
     * @param view
     * @param viewId
     * @param resId
     */
    public void setViewText(View view,int viewId,int resId){
        TextView textView = (TextView) view.findViewById(viewId);
        if (textView!=null){
            textView.setText(resId);
        }
    }

//    public void setViewImage(View view,int viewId,String uri,DisplayImageOptions options){
//        ImageView imageView = (ImageView) view.findViewById(viewId);
//        if (imageView!=null){
//            ImageViewUtils.displayImage(uri, imageView, options);
//        }
//    }

    public void setViewImage(View view,int viewId,int resId){
        ImageView imageView = (ImageView) view.findViewById(viewId);
        if (imageView!=null){
            imageView.setImageResource(resId);
        }
    }

    /**
     * 设置TextView的文本
     * @param view
     * @param viewId
     * @param res
     */
    public void setViewText(View view,int viewId,String res){
        TextView textView = (TextView) view.findViewById(viewId);
        if (textView!=null){
            textView.setText(res);
        }
    }

    public View getViewById(View convertView,int subViewId){
        return convertView.findViewById(subViewId);
    }

    public void setViewVisibility(int visibility,View convertView,int subViewId) {
        View subView = getViewById(convertView,subViewId);
        if (subView!=null){
            subView.setVisibility(visibility);
        }
    }



    // 需要实现的方法获取View
//    public abstract View bindToView(int position,View cellView,T t);
}
